def factorial(number)
	factorial = 0;
	if number == 0
		factorial = 1
	else
		factorial = number * factorial(number - 1)
	end
	factorial
end

def sumOfFactorials(number)
	sumOfFactorials = 0
	number.to_s.scan(/./).each do |digit|
		sumOfFactorials = sumOfFactorials + factorial(digit.to_i(10))
	end
	sumOfFactorials
end

sixtySet = []
(1..1000000).each do |number|
	numberSet = []
	numberSet.push number
	sum = number
	while true
		sum = sumOfFactorials sum
		if !numberSet.include? sum
			numberSet.push sum
		else
			break
		end
	end

	if numberSet.length == 60
		puts number.to_s + " " + numberSet.length.to_s
		sixtySet.push number
	end
end

puts sixtySet.length.to_s